package ooday03;

public class PolymorphicDemo {
    public static void main(String[] args) {
        /*Dog o1=new Dog("小黑",2,"黑");
        //01.name/age/color/eat()/drink()/swim()/lookhome()
        Animal o2 = new Dog("小白",1,"白");
        //o2.name/age/color/eat()/drink();
        Swim o3=new Dog("小灰",2,"灰");
        //o3.swim();

         */
        //演示向上造型（多态）的第一点应用
        /*Dog dog=new Dog("小黑",2,"黑");
        Chick chick=new Chick("小黄",1,"黄");
        Fish fish=new Fish("小金",3,"金");
        Master master=new Master();
        master.feed(dog);     //自动向上造型
        master.feed(chick);
        master.feed(fish);

         */
        //演示向上造型（多态）的第二点应用
        /*Animal[]animals=new Animal[5];
        animals[0]=new Dog("小黑",3,"黑");
        animals[1]=new Dog(C);
        animals[2]=new Chick("小黄",5,"黄");
        animals[3]=new Fish("小绿",2,"绿");
        animals[4]=new Fish("小金",2,"金");
        for (int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();
        }

         */
        //演示强转成功的条件：
       /* Animal o=new Dog("小黑",3,"黑");
        Dog g=(Dog)o;
        Swim s=(Swim) o;
        ((Dog) o).lookHome();
        ((Dog) o).swim();

        System.out.println(o instanceof Dog);
        System.out.println(o instanceof Swim);
        System.out.println(o instanceof Fish);
        if (o instanceof Fish){
        }

        */
        Animal[]animals=new Animal[5];
        animals[0]=new Dog("小黑",3,"黑");
        animals[1]=new Dog("小灰",1,"灰");
        animals[2]=new Chick("小黄",5,"黄");
        animals[3]=new Fish("小绿",2,"绿");
        animals[4]=new Fish("小金",2,"金");
        for (int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].eat();
            animals[i].drink();
            if (animals[i] instanceof Dog){
                Dog dog=(Dog)animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Chick){
                Chick chick=(Chick) animals[i];
                chick.layEggs();
            }
            if (animals[i] instanceof Swim){
                Swim s=(Swim) animals[i];
                s.swim();
            }

            }
    }
}
